#include "ImplicitSphere.h"
#include <vtkActor.h>
#include <vtkContourFilter.h>
#include <vtkImageData.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOutlineFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSampleFunction.h>
#include <vtkSphere.h>

#include <algorithm>
#include <array>
ImplicitSphere::ImplicitSphere(QWidget* parent)
	: QMainWindow(parent)
{
	vtkWidget = new QVTKOpenGLNativeWidget(this);
	this->resize(600, 400);
	this->setCentralWidget(vtkWidget);

	vtkNew<vtkNamedColors> colors;

	// Set the background color.
	std::array<unsigned char, 4> bkg{ {51, 77, 102, 255} };
	colors->SetColor("BkgColor", bkg.data());

	vtkNew<vtkSphere> sphere;

	// Sample the function
	vtkNew<vtkSampleFunction> sample;
	sample->SetSampleDimensions(50, 50, 50);
	sample->SetImplicitFunction(sphere);
	double value = 2.0;
	double xmin = -value, xmax = value, ymin = -value, ymax = value,
		zmin = -value, zmax = value;
	sample->SetModelBounds(xmin, xmax, ymin, ymax, zmin, zmax);

	// Create the 0 isosurface
	vtkNew<vtkContourFilter> contours;
	contours->SetInputConnection(sample->GetOutputPort());
	contours->GenerateValues(1, 1, 1);

	// Map the contours to graphical primitives
	vtkNew<vtkPolyDataMapper> contourMapper;
	contourMapper->SetInputConnection(contours->GetOutputPort());
	contourMapper->ScalarVisibilityOff();

	// Create an actor for the contours
	vtkNew<vtkActor> contourActor;
	contourActor->SetMapper(contourMapper);

	// Visualize
	vtkNew<vtkRenderer> renderer;
	vtkWidget->renderWindow()->AddRenderer(renderer);

	renderer->AddActor(contourActor);
	renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
}
